﻿using System.Data;
using System.Linq;
using System.Text;
using System.IO;

namespace PropertyExpression.SchemaGenerator
{
    static class Program
    {
        static void Main()
        {
            var dataSet = DbUtil.GetSchemaFromDB();

            var tableBuilder = new StringBuilder();
            foreach (var table in dataSet.Tables.Cast<DataTable>()
                .Where(table => !new[]
                                    {
                                        "sysdiagrams"
                                    }.Contains(table.TableName)))
            {
                var columnListBuilder = new StringBuilder();
                foreach (DataColumn column in table.Columns)
                {
                    if (columnListBuilder.Length != 0) columnListBuilder.AppendLine();
                    columnListBuilder.AppendFormat(@"        {0} {1} {{ get; }}", column.DataType, column.ColumnName);
                }
                if (tableBuilder.Length != 0)
                {
                    tableBuilder.AppendLine();
                    tableBuilder.AppendLine();
                }
                tableBuilder.AppendFormat(
                    @"    public interface I{0}
    {{
{1}
    }}",
                    table.TableName,
                    columnListBuilder);
            }

            File.WriteAllText(
                Path.Combine(
                    Path.GetDirectoryName(typeof(Program).Assembly.Location),
                    @"..\..\..\RelationalDataInstance.ServerTest\Schema.cs"),
                string.Format(
                    @"namespace PropertyExpression.RelationalDataInstance.ServerTest
{{
{0}
}}",
                    tableBuilder));
        }
    }
}
